package cn.iocoder.yudao.module.biz.service.expand.targets.customer;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.customer.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.customer.ExpandTargetCustomerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.expand.targets.customer.ExpandTargetCustomerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 拓展目标_客户 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ExpandTargetCustomerServiceImpl implements ExpandTargetCustomerService {

    @Resource
    private ExpandTargetCustomerMapper expandTargetCustomerMapper;

    @Override
    public Long createExpandTargetCustomer(ExpandTargetCustomerSaveReqVO createReqVO) {
        // 插入
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setOwnerUserId(loginUser.getId());
        createReqVO.setDeptId(Long.parseLong(loginUser.getInfo().get("deptId")));
        ExpandTargetCustomerDO expandTargetCustomer = BeanUtils.toBean(createReqVO, ExpandTargetCustomerDO.class);
        expandTargetCustomerMapper.insert(expandTargetCustomer);

        // 返回
        return expandTargetCustomer.getId();
    }

    @Override
    public void updateExpandTargetCustomer(ExpandTargetCustomerSaveReqVO updateReqVO) {
        // 校验存在
        validateExpandTargetCustomerExists(updateReqVO.getId());
        // 更新
        ExpandTargetCustomerDO updateObj = BeanUtils.toBean(updateReqVO, ExpandTargetCustomerDO.class);
        expandTargetCustomerMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpandTargetCustomer(Long id) {
        // 校验存在
        validateExpandTargetCustomerExists(id);
        // 删除
        expandTargetCustomerMapper.deleteById(id);
    }

    @Override
        public void deleteExpandTargetCustomerListByIds(List<Long> ids) {
        // 删除
        expandTargetCustomerMapper.deleteByIds(ids);
        }


    private void validateExpandTargetCustomerExists(Long id) {
        if (expandTargetCustomerMapper.selectById(id) == null) {
            throw exception(BIZ_EXPAND_TARGET_CUSTOMER_NOT_EXISTS);
        }
    }

    @Override
    public ExpandTargetCustomerDO getExpandTargetCustomer(Long id) {
        return expandTargetCustomerMapper.selectById(id);
    }

    @Override
    public PageResult<ExpandTargetCustomerDO> getExpandTargetCustomerPage(ExpandTargetCustomerPageReqVO pageReqVO) {
        return expandTargetCustomerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Integer> getExpandTargetCustomerYear(ExpandTargetCustomerPageReqVO pageReqVO) {
        List<Integer> mapList = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 获取当前年月
        YearMonth currentYearMonth = YearMonth.now();
        // 从当年1月开始到当前月
        for (int month = 1; month <= currentYearMonth.getMonthValue(); month++) {
            String yearMonth = YearMonth.of(currentYearMonth.getYear(), month).format(formatter);
            expandTargetCustomerMapper.getExpandTargetCustomerYear(SecurityFrameworkUtils.getLoginUser().getTenantId()).forEach(b -> {
               if(yearMonth.equals(b.get("month"))){
//                   Map<String,Object> map = new HashMap<>();
//                   map.put(yearMonth,b.get("count"));
                   mapList.add(Integer.valueOf(b.get("count")+""));
               } else {
//                   Map<String,Object> map = new HashMap<>();
//                   map.put(yearMonth,0);
                   mapList.add(0);
               }
            });
        }
        return mapList;
    }

    @Override
    public Integer checkExistByName(String name) {
        return expandTargetCustomerMapper.checkExistByName(name);
    }
}